package com.three.service.impl;

import com.baor.baseUtil.R;
import com.three.dao.ProjectsDao;
import com.three.domain.Projectmembers;
import com.three.domain.Projects;
import com.three.domain.Subprojects;
import com.three.domain.Tasks;
import com.three.service.ProjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectsServiceImpl implements ProjectsService {
    @Autowired
    private ProjectsDao projectsDao;

    @Override
    public R projectsList(String keyword, String start_date, String end_date, Integer pageIndex, Integer pageSize) {
        pageIndex = (pageIndex - 1) * pageSize;
        double PageCountProjects = projectsDao.PageCountProjects(keyword,start_date,end_date);
        PageCountProjects = Math.ceil(PageCountProjects / pageSize);
        List<Projects> projectsList = projectsDao.projectsList(keyword, start_date, end_date, pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("PageCountProjects", PageCountProjects);
        map.put("projectsList", projectsList);
        return R.ok(map);
    }

    @Override
    public R addProjects(Projects projects) {
        return R.ok(projectsDao.addProjects(projects));
    }

    @Override
    public R tasksList(String keyword, String start_date, String end_date, Integer pageIndex, Integer pageSize, Integer project_id, Integer subproject_id) {
        pageIndex = (pageIndex - 1) * pageSize;
        double PageCountTasks = projectsDao.PageCountTasks(keyword,start_date,end_date,project_id,subproject_id);
        PageCountTasks = Math.ceil(PageCountTasks / pageSize);
        List<Tasks> tasksList = projectsDao.tasksList(keyword, start_date, end_date, pageIndex, pageSize,project_id, subproject_id);
        Map<String, Object> map = new HashMap<>();
        map.put("PageCountTasks", PageCountTasks);
        map.put("tasksList", tasksList);
        return R.ok(map);
    }

    @Override
    public R addTasks(Tasks tasks) {
        Integer project_id = tasks.getProject_id();
        Integer subproject_id =tasks.getSubproject_id();
        Double proportion = projectsDao.tasksProportion(project_id, subproject_id);
        if (proportion == null) {
            proportion = tasks.getProportion();
        }
        if(proportion > 100){
            return R.er();
        }
        return R.ok(projectsDao.addTasks(tasks));
    }

    @Override
    public R updateTasks(Tasks tasks) {
        Integer project_id = tasks.getProject_id();
        Integer subproject_id =tasks.getSubproject_id();
        Double proportion = projectsDao.tasksProportion(project_id, subproject_id) + tasks.getProportion();
        if(proportion > 100){
            return R.er();
        }
//        Double subproject_progress = (double) projectsDao.subprojectProgress(subproject_id);
//        projectsDao.updateProgress(subproject_progress,subproject_id);
        return R.ok(projectsDao.updateTasks(tasks));
    }

    @Override
    public R deleteTasks(Integer task_id, Integer subproject_id, Integer project_id) {
        Double subproject_progress = (double) projectsDao.subprojectProgress(subproject_id) / 100;
        projectsDao.updateProgress(subproject_progress,subproject_id);

        Double project_progress = (double)projectsDao.project_progress(subproject_id) / 100;
        projectsDao.updateProjectProgress(project_progress,subproject_id);

        return R.ok(projectsDao.deleteTasks(task_id, subproject_id, project_id));
    }


    @Override
    public R projectsListByTeacher(Map<String, Object> map) {
        String keyword = (String)map.get("keyword");
        String start_date = (String)map.get("start_date");
        String end_date = (String)map.get("end_date");
        int pageIndex = (int) map.get("pageIndex");
        int pageSize = (int) map.get("pageSize");
        int manager_id = (int) map.get("manager_id");

        pageIndex = (pageIndex - 1) * pageSize;
        double PageCountProectsByteacher = projectsDao.PageCountProectsByteacher(manager_id);
        System.out.println(PageCountProectsByteacher);
        PageCountProectsByteacher = Math.ceil(PageCountProectsByteacher / pageSize);
        List<Projects> projectsList = projectsDao.projectsListByteacher(manager_id, keyword, start_date, end_date, pageIndex, pageSize);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("PageCountProectsByteacher", PageCountProectsByteacher);
        map2.put("projectsList", projectsList);
        return R.ok(map2);
    }

    @Override
    public R projectmembersList(Integer project_id) {
        return R.ok(projectsDao.projectsListBytProjectmembers(project_id));
    }

    @Override
    public R notInProjectmembers() {
        return R.ok(projectsDao.notInProjectmembers());
    }

    @Override
    public R addProjectmembers(List<Projectmembers> projectmembers) {
        return R.ok(projectsDao.addProjectmembers(projectmembers));
    }



    @Override
    public R subprojectsList(Integer project_id, Integer pageIndex, Integer pageSize) {
        pageIndex = (pageIndex - 1) * pageSize;
        double PageCountSubprojects = projectsDao.PageCountSubprojects(project_id);
        PageCountSubprojects = Math.ceil(PageCountSubprojects / pageSize);
        List<Subprojects> subprojectsList = projectsDao.subprojectsList(project_id, pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("PageCountSubprojects", PageCountSubprojects);
        map.put("subprojectsList", subprojectsList);
        return R.ok(map);
    }


    @Override
    public R addSubprojects(Subprojects subprojects) {
        Integer project_id = subprojects.getProject_id();
        Double proportion = projectsDao.SubprojectsProportion(project_id);
        if (proportion == null) {
            proportion = 0.00;
        }
        if(proportion > 100){
            return R.er();
        }
        return R.ok(projectsDao.addSubprojects(subprojects));
    }

    @Override
    public R updateTasksByStudent(Tasks tasks) {
        Integer user_id =tasks.getUser_id();
        Integer manager_id = tasks.getManager_id();
        Integer subproject_id =tasks.getSubproject_id();
        if(user_id == manager_id ){
            projectsDao.updateTasksByStudent(tasks);

            Double subproject_progress = (double) projectsDao.subprojectProgress(subproject_id) / 100;
            projectsDao.updateProgress(subproject_progress,subproject_id);

            Double project_progress = (double)projectsDao.project_progress(subproject_id) / 100;
            projectsDao.updateProjectProgress(project_progress,subproject_id);

            return R.ok();
        }
        return R.er();
    }


}
